﻿using FreeRedis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Helper
{
    public class FreeClient
    {
        static string SqliteConfiguration { get => ConfigurationManager.AppSettings["db"]; }
        static string ReidsConfiguration { get => ConfigurationManager.AppSettings["redis"]; }

        static Lazy<IFreeSql> sqliteLazy = new Lazy<IFreeSql>(() =>
        {
            var fsql = new FreeSql.FreeSqlBuilder()
                .UseMonitorCommand(cmd => Trace.WriteLine($"Sql：{cmd.CommandText}"))
                .UseAdoConnectionPool(true)
                .UseConnectionString(FreeSql.DataType.Sqlite, SqliteConfiguration)
                .UseAutoSyncStructure(true) //自动同步实体结构到数据库，只有CRUD时才会生成表
                .Build();
            return fsql;
        });

        static Lazy<RedisClient> redisClient = new Lazy<RedisClient>(() =>
        {
            var client = new RedisClient(ReidsConfiguration);
            client.Notice += (s, e) => 
            {
                Console.WriteLine(e.Log);
            };
            return client;
        }); 

        public static IFreeSql Sqlite => sqliteLazy.Value;

        public static RedisClient Redis => redisClient.Value;
    }
}
